package com.test.stream;

import cn.hutool.core.util.StrUtil;
import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class StreamDemo {

    public static void main(String[] args) {
        List<Car> carList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            carList.add(new Car((long)i, "车辆---" + i, 4));
        }

        List<Long> idList = carList.stream().map(Car::getId).collect(Collectors.toList());
        System.out.println(Joiner.on(",").join(idList));

        Long[] ids = carList.stream().map(Car::getId).toArray(Long[]::new);
        System.out.println(Joiner.on(",").join(ids));


        List<Car2> car2List = carList.stream().map(c -> {
            Car2 c2 = new Car2(c.getId(), c.getName(), c.getWheelNum());
            return c2;
        }).collect(Collectors.toList());

        car2List.stream().forEach(c -> {
            System.out.println(StrUtil.format("carId={}, carName={}, wheelNum={}", c.getId(), c.getName(), c.getWheelNum()));
        });

    }

    private void test1() {

        List<String> strs = Arrays.asList("1", "2", "3", "4", "5","6", "7");
        strs.parallelStream().forEach(str -> {
            System.out.println(Thread.currentThread().getId() + " --- " + str);
        });

        strs.stream().forEach(str -> {
            System.out.println(Thread.currentThread().getId() + " --- " + str);
        });

        List<Car> carList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            carList.add(new Car((long)i, "车辆---" + i, 4));
        }
        carList.stream().filter(c -> c.getId() % 2 == 0).map(c -> {
            c.setName("车辆");
            return c;
        }).collect(Collectors.toList());

//        carList.stream().forEach(c -> System.out.println(c.getName()));
        for (Car c : carList) {
            System.out.println(c.getName());
        }
    }
}
